//
//  InMemoryFileTest.cpp
//  FuseSQLite
//
//  Created by Admin on 6/22/14.
//  Copyright (c) 2014 Admin. All rights reserved.
//

#include "InMemoryFile.h"

#include <gtest/gtest.h>

TEST(InMemoryFile, readWithZeroSize)
{
    std::vector<char> fileData;
    
    InMemoryFile file(&fileData);
    
    size_t read = file.read(nullptr, 1, 0);
    EXPECT_EQ(0, read);
}

TEST(InMemoryFile, read)
{
    char bufferStr[] = "12345678910111213";
    std::vector<char> fileData(std::begin(bufferStr), std::end(bufferStr));
    
    InMemoryFile file(&fileData);
    
    char resultBuffer[200] = {0};
    
    size_t read = file.read(resultBuffer, sizeof(bufferStr), 0);
    EXPECT_EQ(sizeof(bufferStr), read);
    EXPECT_STREQ("12345678910111213", resultBuffer);
    
    read = file.read(resultBuffer, 200, 0);
    EXPECT_EQ(sizeof(bufferStr), read);
    EXPECT_STREQ(bufferStr, resultBuffer);
    
    read = file.read(resultBuffer, 200, 2);
    EXPECT_EQ(sizeof(bufferStr) - 2, read);
    EXPECT_STREQ("345678910111213", resultBuffer);
    
    read = file.read(resultBuffer, 2, 200);
    EXPECT_EQ(0, read);
}

TEST(InMemoryFile, write)
{
    std::vector<char> fileData;
    InMemoryFile file(&fileData);

    char str1[] = "123";
    size_t written = file.write(str1, sizeof(str1), 0);
    EXPECT_EQ(sizeof(str1), written);
    EXPECT_STREQ(str1, fileData.data());
    
    char str2[] = "12345";
    written = file.write(str2, sizeof(str2), 0);
    EXPECT_EQ(sizeof(str2), written);
    EXPECT_STREQ(str2, fileData.data());
}

TEST(InMemoryFile, writeAfterWrite)
{
    std::vector<char> fileData;
    InMemoryFile file(&fileData);
    
    char str1[] = "123";
    file.write(str1, sizeof(str1), 0);
    
    char str2[] = "4321";
    file.write(str2, sizeof(str2), sizeof(str1) - 1);

    EXPECT_STREQ("1234321", fileData.data());
}

TEST(InMemoryFile, write_over_written)
{
    std::vector<char> fileData;
    InMemoryFile file(&fileData);
    
    file.write("123", 4, 0);
    
    file.write("000", 4, 0);
    
    EXPECT_STREQ("000", fileData.data());
}
